package com.bruce.pullrefresh.xListView;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bruce.pullrefresh.R;


public class XListViewFooter extends LinearLayout {
	public final static int STATE_NORMAL = 0;
	public final static int STATE_READY = 1;
	public final static int STATE_LOADING = 2;
	private View mContentView;
	private View mProgressBar;
	private TextView mHintView;
	private int mInitHeight;
	private int mState = STATE_NORMAL;

	public XListViewFooter(Context context) {
		super(context);
		initView(context);
	}

	public XListViewFooter(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}

	public void setState(int state) {
		mState = state;
		mHintView.setVisibility(View.INVISIBLE);
		mProgressBar.setVisibility(View.INVISIBLE);
		if (state == STATE_READY) {
			mHintView.setVisibility(View.VISIBLE);
			mHintView.setText(R.string.xlv_footer_hint_ready);
		} else if (state == STATE_LOADING) {
			mHintView.setVisibility(View.VISIBLE);
			mProgressBar.setVisibility(View.VISIBLE);
			mHintView.setText(R.string.xlv_footer_hint_loading);
		} else {
			mHintView.setVisibility(View.VISIBLE);
			mHintView.setText(R.string.xlv_footer_hint_normal);
		}
	}
	
	public int getState(){
		return mState;
	}

	public void setBottomMargin(int height) {
		if (height < 0){
			return;
		}
		LayoutParams lp = (LayoutParams) mContentView
				.getLayoutParams();
		lp.bottomMargin = height;
		mContentView.setLayoutParams(lp);
	}

	public int getBottomMargin() {
		LayoutParams lp = (LayoutParams) mContentView
				.getLayoutParams();
		return lp.bottomMargin;
	}

//	/**
//	 * normal status
//	 */
//	public void normal() {
//		mHintView.setVisibility(View.VISIBLE);
//		mProgressBar.setVisibility(View.GONE);
//	}
//
//	/**
//	 * loading status
//	 */
//	public void loading() {
//		mHintView.setVisibility(View.GONE);
//		mProgressBar.setVisibility(View.VISIBLE);
//	}

	/**
	 * hide footer when disable pull load more
	 */
	public void hide() {
		LayoutParams lp = (LayoutParams) mContentView
				.getLayoutParams();
		lp.height = 0;
		mContentView.setLayoutParams(lp);
	}

	/**
	 * show footer
	 */
	public void show() {
		LayoutParams lp = (LayoutParams) mContentView
				.getLayoutParams();
		lp.height = mInitHeight;
		mContentView.setLayoutParams(lp);
	}

	private void initView(Context context) {
		DisplayMetrics dm = getContext().getResources().getDisplayMetrics();
		float dens = dm != null ? dm.density : 2.0f;
		mInitHeight = (int) (45 * dens);
		View moreView = LayoutInflater.from(context).inflate(
				R.layout.widget_xlistview_footer, this, false);
		addView(moreView);
		moreView.setLayoutParams(new LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		mContentView = moreView.findViewById(R.id.xlv_footer_content);
		mProgressBar = moreView.findViewById(R.id.xlv_footer_proBar);
		mHintView = (TextView) moreView.findViewById(R.id.xlv_footer_hint_tv);
	}
}
